package com.example.app17_motionevent;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

    private static final String TAG = "MainActivity";

    private ImageView ivMain;

    private LinearLayout parentView;

    private int lastX;

    private int lastY;

    private int maxRight;

    private int maxBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");

        ivMain = findViewById(R.id.iv_main);
        parentView = (LinearLayout) ivMain.getParent();
        ivMain.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // 获取事件的坐标
        int eventX = (int) event.getRawX();
        int eventY = (int) event.getRawY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 获取父视图的right和bottom
                if (maxRight == 0) {
                    maxRight = parentView.getRight();
                    maxBottom = parentView.getBottom();
                }
                lastX = eventX;
                lastY = eventY;
                break;
            case MotionEvent.ACTION_MOVE:
                // 计算事件的偏移
                int dx = eventX - lastX;
                int dy = eventY - lastY;
                // 根据事件的偏移移动ImageView
                int left = ivMain.getLeft() + dx;
                int top = ivMain.getTop() + dy;
                int right = ivMain.getRight() + dx;
                int bottom = ivMain.getBottom() + dy;

                // 限制left >= 0
                if (left < 0) {
                    right += -left;
                    left = 0;
                }

                // 限制top
                if (top < 0) {
                    bottom += -top;
                    top = 0;
                }

                // 限制right
                if (right <= maxRight) {
                    left -= right - maxRight;
                    right = maxRight;
                }

                // 限制bottom
                if (bottom <= maxBottom) {
                    top -= bottom - maxBottom;
                    bottom = maxBottom;
                }

                ivMain.layout(left, top, right, bottom);
                // 重新记录lastX和lastY的值
                lastX = eventX;
                lastY = eventY;
                break;
        }
        // 所有的MotionEvent都交给ImageView处理
        return true;
    }
}